/**
 * 
 */
var noticeCount = {"count_not_read":0, "count_sended":0, "count_draft":0};
var noticeToolbar = new Array();
NoticeList = function(){
	return {
		code : "0",
		init : function(code){
			NoticeList.initNoticeCount(code);
//			NoticeList.initNoticeListDataGrid();
		},
		// 初始化通知公告数据
		initNoticeListDataGrid : function(){
			$('#noticeListDataGrid').datagrid({
				url				:	basePath+'/pc/notice/getNoticePageData.htm',
				fit				:	true,
				fitColumns		:	true,
				striped			:	true,
				rownumbers		:	true,
				autoRowHeight	:	true,
				nowrap			:	true,
				pagination		:	true,
				method 			:	'POST',
				loadMsg			:	"载入中...",
				columns			: [[
				    {field:'newsId',title:'newsId',checkbox:true,width:100},
				    {field:'title',title:'标题',width:100,formatter:function(value,rowData,index){
				    	if(value.length > 20){
				    		value = value.substring(0, 20) + '...';
				    	}
				    	if("0" == rowData.state || "2" == rowData.state){
				    		return '<a href="javascript:void(0)" onclick="NoticeList.showDraftDetail(\''+rowData.newsId+'\')">'+value+'</a>';
				    	}
				    	if(0 == rowData.readNum){
			    			value = value+"<font color='red' id='font"+rowData.newsId+"'><b> &nbsp;New</b></font>";
			    			return '<a href="javascript:void(0)" onclick="NoticeList.showNewDetail(\''+rowData.newsId+'\', 0)">'+value+'</a>';
			    		} else {
			    			return '<a href="javascript:void(0)" onclick="NoticeList.showNewDetail(\''+rowData.newsId+'\')">'+value+'</a>';
			    		}
				    }},
				    {field:'employeeName',title:'发布人',width:100},
				    {field:'releaseDate',title:'发布时间',width:100}
				]],
				toolbar : noticeToolbar
			});
			NoticeList.initSearchBox();
		},
		// 初始化各通知条数
		initNoticeCount : function(code){
			$.ajax({
				url		:	basePath+"/pc/notice/getNoticeCount.htm",
				data	:	{"newsType" : '0'},
				type	:	'POST',
				dataType:	'JSON',
				success :	function(data) {
					if(data){
						noticeCount.count_not_read = data.count_not_read;
						noticeCount.count_sended = data.count_sended;
						noticeCount.count_draft = data.count_draft;
						NoticeList.initToolbar(code);
					}
				}
			});
		},
		// 初始化工具栏
		initToolbar : function(code){
			NoticeList.code = code;
			noticeToolbar = [
                {
                	 text : "全部",
                	 iconCls : 'add',
                	 handler : function() {
                		 $('#noticeListDataGrid').datagrid({queryParams:{}});
                		 NoticeList.initToolbar("0");
                	 }
                },{
			    	text : "未阅("+noticeCount.count_not_read+")",
					iconCls : 'add',
					handler : function() {
						$('#noticeListDataGrid').datagrid({queryParams:{code : '1'}});
						NoticeList.initToolbar("1");
					}
			    },{
			    	text : "已发("+noticeCount.count_sended+")",
					iconCls : 'add',
					handler : function() {
						$('#noticeListDataGrid').datagrid({queryParams:{code : '2'}});
						NoticeList.initToolbar("2");
					}
			    },{
			    	text : "草稿("+noticeCount.count_draft+")",
					iconCls : 'add',
					handler : function() {
						$('#noticeListDataGrid').datagrid({queryParams:{code : '3'}});
						NoticeList.initToolbar("3");
					}
			    },{
			    	text : "新增发布",
					iconCls : 'add',
					handler : function() {
						Ict.openWin2('新增发布', 900, 500, basePath+"/pc/notice/addNotice.htm");
					}
			    },{
			    	text : "刷新",
					iconCls : 'reload',
					handler : function() {
						NoticeList.doRefresh();
					}
			    }
			];
			if("3" == code){
				noticeToolbar.push({
					text : "删除",
					iconCls : 'remove',
					handler : function() {
						NoticeList.doDelete();
					}
				});
			}
			NoticeList.initNoticeListDataGrid();
			
		},
		// 在toolbar上面添加搜索框
		initSearchBox : function(width) {
			$('.searchbox').css({
				'width' : width + 'px'
			});
			$('.searchbox-text').css({
				'width' : eval(width - 20) + 'px'
			});
			$('.datagrid-toolbar').append($('.serarchbox'));
			$('.datagrid-toolbar').append($('#tbar'));
		},
		doRefresh:function(){
			NoticeList.init(NoticeList.code);
		},
		doDelete : function(){
			var rows = $('#noticeListDataGrid').datagrid("getSelections");
			if(!rows || 0 == rows.length){
				Ict.warn("请选择!");
   				return;
			}
			var ids = new Array();
			for(var i = 0; i < rows.length; i ++){
				ids.push(rows[i].newsId);
			}
			Ict.confirm("是否删除已选?", function(yes){
				if(yes){
					$.ajax({
						url		:	basePath+"/pc/notice/deleteNotice.htm?ids="+ids,
						type	:	'POST',
						dataType:	'JSON',
						async	:	false,
						success :	function(data) {
							if(data && 0 == data.error){
								Ict.info("删除成功");
							} else if(1 == data.error){
								Ict.warn("【"+data.message+"】已发布,请取消选择后再执行删除操作");
							}
						}
					});
				}
			});
		},
		//查看通知公告
		showNewDetail : function(newsId, readNum){
			Ict.openWin('通知公告信息',820,420,"/pc/notice/noticeDetail/"+newsId+".htm");
			if(0 == readNum) {
				setTimeout(function(){
					NoticeList.init();
				}, 500);
			}
		},
		showDraftDetail : function(newsId){
			Ict.openWin2('草稿信息',900,500,"/pc/notice/noticeEdit/"+newsId+".htm");	
		},
		doSearch : function(value) {
			$('#noticeListDataGrid').datagrid({
				queryParams : {
					'search'	: value,
					"code"		: NoticeList.code
				},
				toolbar : '#toolBar'
			});
		}
	};
}();

$(function(){
	NoticeList.init("0");
});